﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace dllLink
{
    internal abstract class Estimator
    {
            internal Product mvProduct;
            internal TimeSpan mvHistoryDepth;
            internal DateTime mvPricingDate;
   
            // fonctions utiles aux Estimateurs :

            protected double[] Cotation(string theName, DateTime theDate, TimeSpan theHistoryLength)
            {
                moteurbd myBD = new moteurbd();
                string myDate = theDate.Year + "/" + theDate.Month + "/" + theDate.Day;
                DateTime myFirstDate = theDate.Subtract(theHistoryLength);
                string dateInf = myFirstDate.Year + "/" + myFirstDate.Month + "/" + myFirstDate.Day;

                string select = theName + "." + moteurbd.cotation;
                string from = theName;
                string date = theName + "." + moteurbd.date;
                string where = date + "<='" + myDate + "' and " + date + ">='" + dateInf + "'";

                double[] result;
                int i = 0;

                myBD.Connect();
                AdpDts adp = myBD.Select(select, from, where);
                myBD.Disconnect();

                DataTable dt = adp.dts.Tables[0];
                DataRow[] dr = dt.Select();

                result = new double[dr.GetLength(0)];

                foreach (DataRow r in dt.Select())
                {
                    result[i] = Convert.ToDouble(r[0].ToString());
                    i++;
                }
                return result;
            }
            

            protected double[,] CotationAtSameDates(string theName1, string theName2, DateTime theDate, TimeSpan theHistoryLength)
            { 
                moteurbd myBD = new moteurbd();
                string date;
                date = theDate.Year + "/" + theDate.Month + "/" + theDate.Day;
                DateTime FirstDate = theDate.Subtract(theHistoryLength);
                string dateInf = FirstDate.Year + "/" + FirstDate.Month + "/" + FirstDate.Day;
                string select, from, date1, date2, where;
                bool isVariance = (theName1 == theName2);
                if (isVariance)
                {
                    select = theName1 + "." + moteurbd.cotation;
                    from = theName1;
                    date1 = theName1 + "." + moteurbd.date;
                    where = date1 + "<='" + date + "' and " + date1 + ">='" + dateInf + "'";
                }
                else
                {
                    select = theName1 + "." + moteurbd.cotation +", " + theName2 + "." + moteurbd.cotation;
                    from = theName1 + ", " + theName2;
                    date1 = theName1 + "." + moteurbd.date;
                    date2 = theName2 + "." + moteurbd.date;
                    where = date1 + "=" + date2 + " and " + date1 + "<='" + date + "' and " + date2 + "<='" + date +
                            "'" + " and " + date1 + ">='" + dateInf + "' and " + date2 + ">='" + dateInf + "'";
                }

                double[,] result;
                int i = 0;

                myBD.Connect();
                AdpDts adp = myBD.Select(select,from ,where);
                myBD.Disconnect();
                
                DataTable dt = adp.dts.Tables[0];
                DataRow[] dr = dt.Select();

                result = new double[dr.GetLength(0),2];

                if (isVariance)
                {
                    foreach (DataRow r in dt.Select())
                    {
                        result[i, 0] = Convert.ToDouble(r[0].ToString());
                        result[i, 1] = result[i, 0];
                        i++;
                    }
                }
                else
                {
                    foreach (DataRow r in dt.Select())
                    {
                        result[i, 0] = Convert.ToDouble(r[0].ToString());
                        result[i, 1] = Convert.ToDouble(r[1].ToString());
                        i++;
                    }
                }
                return result;
            }


            protected double[,] ReturnsAtSameDates(string theName1, string theName2, DateTime theDate, TimeSpan theHistoryLength)
            {
                moteurbd myBD = new moteurbd();
                string date;
                date = theDate.Year + "/" + theDate.Month + "/" + theDate.Day;
                DateTime FirstDate = theDate.Subtract(theHistoryLength);
                string dateInf = FirstDate.Year + "/" + FirstDate.Month + "/" + FirstDate.Day;
                string select, from, date1, date2, where;
                bool isVariance = (theName1 == theName2);
                if (isVariance)
                {
                    select = theName1 + "." + moteurbd.rendement;
                    from = theName1;
                    date1 = theName1 + "." + moteurbd.date;
                    where = date1 + "<='" + date + "' and " + date1 + ">='" + dateInf + "'";
                }
                else
                {
                    select = theName1 + "." + moteurbd.rendement + ", " + theName2 + "." + moteurbd.rendement;
                    from = theName1 + ", " + theName2;
                    date1 = theName1 + "." + moteurbd.date;
                    date2 = theName2 + "." + moteurbd.date;
                    where = date1 + "=" + date2 + " and " + date1 + "<='" + date + "' and " + date2 + "<='" + date +
                            "'" + " and " + date1 + ">='" + dateInf + "' and " + date2 + ">='" + dateInf + "'";
                }

                double[,] result;
                int i = 0;

                myBD.Connect();
                AdpDts adp = myBD.Select(select, from, where);
                myBD.Disconnect();

                DataTable dt = adp.dts.Tables[0];
                DataRow[] dr = dt.Select();

                result = new double[dr.GetLength(0), 2];

                if (isVariance)
                {
                    foreach (DataRow r in dt.Select())
                    {
                        result[i, 0] = Convert.ToDouble(r[0].ToString());
                        result[i, 1] = result[i, 0];
                        i++;
                    }
                }
                else
                {
                    foreach (DataRow r in dt.Select())
                    {
                        result[i, 0] = Convert.ToDouble(r[0].ToString());
                        result[i, 1] = Convert.ToDouble(r[1].ToString());
                        i++;
                    }
                }
                return result;
            }


            // récupère une colonne d'une matrice
            internal double[] GetUnderlyingData(double[,] theHisto, int theUnderlyingIndex)
            {
                double[] res = new double[theHisto.GetLength(0)];
                for (int i = 0; i < res.Length; i++)
                {
                    res[i] = theHisto[i, theUnderlyingIndex];
                }
                return res;
            }



            //public double FraisOPCVM1(double value)
            //{
            //    return value * (1 + 1.65 / 100);
            //}
            //public double FraisOPCVM2(double value)
            //{
            //    return value * (1 + 2 / 100);
            //}
            //public double FraisOPCVM3(double value)
            //{
            //    return value * (1 + 1.60 / 100);
            //}
            //public double FraisOPCVM4(double value)
            //{
            //    return value * (1 + 2.10 / 100);
            //}
            //public double FraisOPCVM5(double value)
            //{
            //    return value * (1 + 1.65 / 100);
            //}
            //public double FraisOPCVM6(double value)
            //{
            //    return value * (1 + 2 / 100);
            //}
            //public double FraisOPCVM7(double value)
            //{
            //    return value * (1 + 1.65 / 100);
            //}
            //public double FraisOPCVM8(double value)
            //{
            //    return value * (1 + 1.25 / 100);
            //}
            //public double FraisOPCVM9(double value)
            //{
            //    return value * (1 + 0.90 / 100);
            //}
            //public double FraisOPCVM10(double value)
            //{
            //    return value * (1 + 0.90 / 100);
            //}
    }
}
